﻿@inject IDataCacheService _dataCacheService
@inject IHttpService _httpService
@inject IPopupService PopupService

@if (Model.Any())
{
    <CnGalWebSite.Components.Containers.MasaTitleContainer Icon="@IconType.Edit.ToIconString()" Title="近期修改" Class="mb-4" Outline>
        <ButtonContent>
            <MButton Text Block Color="success" OnClick="ReviewAll">
                <MIcon Left>mdi-read </MIcon>
                全部已读
            </MButton>
        </ButtonContent>
        <ChildContent>
            @foreach (var item in Model.Skip((CurrentPage - 1) * MaxCount).Take(MaxCount))
            {

                <div class="mb-4">
                    <ExamineCard Model="item" OnReviewed="OnReviewed" InReview />
                </div>
            }

            @if (TotalPages > 1)
            {
                <CnGalWebSite.Components.Paginations.MasaPagination Value="@CurrentPage" Length="@TotalPages" ValueChanged="OnPageClick" />
            }
        </ChildContent>


    </CnGalWebSite.Components.Containers.MasaTitleContainer>
}


@code {
    [Parameter]
    public List<ExaminedNormalListModel> Model { get; set; } = new List<ExaminedNormalListModel>();

    public StringNumber TabIndex { get; set; } = 1;

    public int MaxCount { get; set; } = 10;

    public int TotalPages => ((Model.Count - 1) / MaxCount) + 1;

    public int CurrentPage { get; set; } = 1;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    private void OnPageClick(int pageIndex)
    {
        CurrentPage = pageIndex;
        StateHasChanged();
        
    }

    public async Task ReviewAll()
    {
        try
        {
            var obj = await _httpService.PostAsync<EditUserReviewEditRecordStateModel, Result>("api/examines/EditUserReviewEditRecordState", new EditUserReviewEditRecordStateModel
            {
                State = EditRecordReviewState.Reviewed,
                ExamineIds = Model.Select(s => s.Id).ToArray()
            });

            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastAsync("审阅失败，" + obj.Error, AlertTypes.Error);
            }
            else
            {
                Model.Clear();
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {

            await ErrorHandler.ProcessError(ex, "审阅失败");
        }
    }

    public void OnReviewed(ExaminedNormalListModel model)
    {
        Model.Remove(model);
        StateHasChanged();
        
    }
}
